package com.lagou.mr.workhome;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class SortDriver {
    public static void main(String[] args) {
        try {
            // 1.获取配置文件对象，获取Job对象实例
            Configuration conf = new Configuration();
            Job job = Job.getInstance(conf,"SortDriverJob");
            // 2 指定程序jar的本地路径
            job.setJarByClass(SortDriver.class);
            // 3 指定Mapper/Reducer类
            job.setMapperClass(SortMapper.class);
            job.setReducerClass(SortReducer.class);

            // 4 指定Mapper/Reducer输出的kv类型
            job.setMapOutputKeyClass(IntWritable.class);
            job.setMapOutputValueClass(NullWritable.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);

            // 6 指定job处理的原始数据路径
            FileInputFormat.setInputPaths(job,new Path("D:\\input"));
            // 7 指定job结果输出路径
            FileOutputFormat.setOutputPath(job,new Path("D:\\output"));
            //8 设置reduceTask的数量
            job.setNumReduceTasks(1);
            //8 提交作业
            boolean flag = job.waitForCompletion(true);
            // jvm退出：正常退出是0，非0则是错误退出
            System.exit(flag ? 0 : 1);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
